package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.DoctorAdvice;
import com.his.entity.HisUser;
import com.his.mapper.DoctorAdviceMapper;
import com.his.service.DoctorAdviceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (DoctorAdvice)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:07:46
 */
@Transactional()
@Service("doctorAdviceService")
public class DoctorAdviceServiceImpl implements DoctorAdviceService {
    @Resource
    private DoctorAdviceMapper doctorAdviceMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DoctorAdvice queryById(Integer id) {
        return this.doctorAdviceMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param pageParam 分页对象
     * @return 查询的结果
     */
    @Override
    public Rsp queryAll(PageParam<DoctorAdvice> pageParam, HisUser user) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        DoctorAdvice model = pageParam.getModel();
        if(user.getRoles().contains("doctor")){
            model.setDoctorId(user.getId());
        }
        PageInfo<DoctorAdvice> info = new PageInfo<>(doctorAdviceMapper.queryAll(model));
        return Rsp.success(info);
    }

    /**
     * 新增数据
     *
     * @param doctorAdvice 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(DoctorAdvice doctorAdvice) {
        return this.doctorAdviceMapper.insert(doctorAdvice)>0?Rsp.success(null):Rsp.fail().msg("添加失败");
    }

    /**
     * 修改数据
     *
     * @param doctorAdvice 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(DoctorAdvice doctorAdvice) {
        return this.doctorAdviceMapper.update(doctorAdvice)>0?Rsp.success(null):Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.doctorAdviceMapper.deleteById(id) > 0;
    }

    @Override
    public List<DoctorAdvice> selectAllByPatientId(String patientId) {
        List<DoctorAdvice> doctorAdviceList=doctorAdviceMapper.queryALLByPatientId(Integer.parseInt(patientId));
        return doctorAdviceList;
    }
}
